
#import "GetTorrentDetailsRequest.h"

@implementation GetTorrentDetailsRequest

//
// torrent info
//
@synthesize torrentInfo														= _torrentInfo;

//
// init
//
- (id)init
{
	//
	// call super
	//
	self																	= [super init];
	if(!self)
		return nil;
	
	//
	// create xml parser, torrent info
	//
	self.xmlParser															= [[[XmlParser alloc] init] autorelease];
	self.torrentInfo														= [[[TorrentInfo alloc] init] autorelease];
	
	return self;
}

//
// dealloc
//
- (void)dealloc
{
	self.torrentInfo														= nil;
	
	[super dealloc];
}

//
// setup request
//
- (void)setupRequest
{
	//
	// setup relpath
	//
	self.relPath															= [NSString stringWithFormat:@"details.php?id=%d", self.torrentInfo.torrentId];
	
	//
	// call super
	//
	[super setupRequest];
}

//
// finished
//
- (void)connectionDidFinishLoading:(NSURLConnection*)connection
{
	if(self.response.statusCode == 200)
	{
		[self.xmlParser parseDocumentData:self.dataBuffer htmlMode:YES];
		
		NSString* temp														= nil;
		xmlNodePtr basicInfoRow												= 0;
		for(NSUInteger i = 2; i < 5; i ++)
		{
			temp															= [self.xmlParser stringFromPath:[NSString stringWithFormat:@"//h1[@id='top']/following-sibling::table[1]/tr[%u]/td[1]", i] startNode:0];
			if([temp isEqualToString:@"Basic Info"])
			{
				basicInfoRow												= [self.xmlParser nodeFromPath:[NSString stringWithFormat:@"//h1[@id='top']/following-sibling::table[1]/tr[%u]", i] startNode:0];
				break;
			}
		}
		
		self.torrentInfo.uploadDate											= [self.xmlParser stringFromPath:@"./td[2]/span[last()]/@title" startNode:basicInfoRow];
		if(!self.torrentInfo.uploadDate.length)
		{
			NSError* error													= nil;
			NSRegularExpression* regex										= [NSRegularExpression regularExpressionWithPattern:@"Uploaded.*(\\d{4}\\-\\d{2}\\-\\d{2} \\d{2}:\\d{2}:\\d{2})" options:NSRegularExpressionAnchorsMatchLines error:&error];
			NSString* temp													= [self.xmlParser stringFromPath:@"./td[2]" startNode:basicInfoRow];
			NSTextCheckingResult* match										= !temp || error || !regex ? nil : [regex firstMatchInString:temp options:0 range:NSMakeRange(0, temp.length)];
			if(match)
				self.torrentInfo.uploadDate									= [temp substringWithRange:[match rangeAtIndex:1]];
		}
		
		temp																= [self.xmlParser stringFromPath:@"./td[2]" startNode:basicInfoRow];
		if(temp)
		{
			NSRange typeRange												= [temp rangeOfString:@"Type:"];
			self.torrentInfo.category										= [[temp substringFromIndex:typeRange.location + typeRange.length + 1] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
			NSRange sizeRange												= [temp rangeOfString:@"Size:"];
			self.torrentInfo.size											= [[temp substringWithRange:NSMakeRange(sizeRange.location + sizeRange.length + 1, typeRange.location - sizeRange.location - sizeRange.length - 1)] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
			self.torrentInfo.size											= [self.torrentInfo.size stringByReplacingOccurrencesOfString:@" " withString:@""];
		}
		
		temp																= [self.xmlParser stringFromPath:@"./following-sibling::tr[1]/td[1]" startNode:basicInfoRow];
		xmlNodePtr promotionNode											= 0;
		if([temp isEqualToString:@"Promotion"])
			promotionNode													= [self.xmlParser nodeFromPath:@"./following-sibling::tr[1]/td[2]" startNode:basicInfoRow];
		else
			promotionNode													= [self.xmlParser nodeFromPath:@"./following-sibling::tr[2]/td[2]" startNode:basicInfoRow];
		self.torrentInfo.magicExpire										= [self.xmlParser stringFromPath:@"./b[last()]/span/@title" startNode:promotionNode];
		self.torrentInfo.upRate												= [self.xmlParser stringFromPath:@"./img[@class='arrowup']/following-sibling::b[1]" startNode:promotionNode];
		self.torrentInfo.downRate											= [self.xmlParser stringFromPath:@"./img[@class='arrowdown']/following-sibling::b[1]" startNode:promotionNode];
		if(!self.torrentInfo.upRate && !self.torrentInfo.downRate)
			self.torrentInfo.upRate											= [NexusClient parseFreeTypeForImgClass:[self.xmlParser stringFromPath:@"./img[@class!='arrowup' and @class!='arrowdown']/@class" startNode:promotionNode]];
		
		temp																= [self.xmlParser stringFromPath:@"//div[@id='peercount']" startNode:0];
		if(temp)
		{
			NSArray* components												= [temp componentsSeparatedByString:@" | "];
			self.torrentInfo.seederCount									= [[components objectAtIndex:0] integerValue];
			self.torrentInfo.leecherCount									= [[components objectAtIndex:1] integerValue];
		}
		self.torrentInfo.torrentDescription									= [self.xmlParser rawContentFromPath:@"//div[@id='kdescr']" startNode:0];
		self.torrentInfo.aniDB												= [self.xmlParser rawContentFromPath:@"//div[@id='kanidb']" startNode:0];
		self.torrentInfo.unread												= NO;
	}
	
	[super connectionDidFinishLoading:connection];
}

@end

